package com.gaowl.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaowl.yygh.cmn.client.DictFeignClient;
import com.gaowl.yygh.enums.DictEnum;
import com.gaowl.yygh.model.user.Patient;
import com.gaowl.yygh.user.mapper.PatientMapper;
import com.gaowl.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: appointment-register
 * @description: 就诊人服务接口实现类
 * @author: gaowl@stu.jiangnan.edu.cn
 * @create: 2021-04-07 21:02
 **/
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    // 根据userId获取获取就诊人列表
    @Override
    public List<Patient> findAllUserId(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        // TODO:通过远程调用，查询编码对应具体内容
        System.out.println(patientList);
        patientList.forEach(this::packPatient);

        return patientList;
    }

    // 根据id获取就诊人信息
    @Override
    public Patient getPatientId(Long id) {
        return this.packPatient(baseMapper.selectById(id));
    }

    /**
    * @Description: Patient对象里面其他参数封装
    * @Param: 封装前的Patient对象
    * @return: 封装后的Patient对象
    * @Author: gaowl@stu.jiangnan.edu.cn
    * @Date: 2021/04/07
    */
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型、联系人证件类型
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        String contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());

        String provinceString = dictFeignClient.getName(patient.getProvinceCode()); // 省
        String cityString = dictFeignClient.getName(patient.getCityCode());         // 市
        String districtString = dictFeignClient.getName(patient.getDistrictCode()); // 区

        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
